package com.meta.builder.schema.config;

import com.meta.builder.schema.rule.MetaRuleFactory;
import com.sun.codemodel.JCodeModel;
import org.jsonschema2pojo.*;

import java.io.IOException;
import java.nio.file.Files;

public class MetaJsonschema2pojoConfig {

    /**
     * 总体配置
     *
     * @param includeGetAndSetFlag 是否需要get\set代码
     * @return
     */
    public static GenerationConfig getGenerationConfig(boolean includeGetAndSetFlag) {
        return new DefaultGenerationConfig() {

            @Override
            public boolean isIncludeAllPropertiesConstructor() {
                return false;
            }

            /**
             * 使用json内容进行构建javaBean
             * @return
             */
            @Override
            public SourceType getSourceType() {
                return SourceType.JSONSCHEMA;
            }

            @Override
            public boolean isGenerateBuilders() { // set config option by overriding method
                return true;
            }

            @Override
            public AnnotationStyle getAnnotationStyle() {
                return AnnotationStyle.NONE;
            }


            @Override
            public boolean isIncludeAdditionalProperties() {
                return true;
            }

            @Override
            public boolean isIncludeGetters() {
                return true;
            }

            @Override
            public boolean isIncludeSetters() {
                return true;
            }

            @Override
            public boolean isIncludeToString() {
                return true;
            }


            @Override
            public boolean isSerializable() {
                return true;
            }

            @Override
            public boolean isIncludeGeneratedAnnotation() {
                return true;
            }

            @Override
            public boolean isIncludeHashcodeAndEquals() {
                return false;
            }

            @Override
            public String getTargetVersion() {
                return "1.8";
            }

            @Override
            public InclusionLevel getInclusionLevel() {
                return InclusionLevel.ALWAYS;
            }
        };
    }

    public static GenerationConfig getGenerationConfig() {
        return getGenerationConfig(false);
    }

    /**
     * 生成的注解配置
     *
     * @param generationConfig
     * @return
     */
    public static MetaAbstractTypeInfoAnnotator getAnnotator(GenerationConfig generationConfig) {
        return new MetaAbstractTypeInfoAnnotator(generationConfig);
    }


    /**
     * 自定义总体配置+注解配置
     *
     * @return
     */
    public static SchemaMapper getSchemaMapper(GenerationConfig config, MetaAbstractTypeInfoAnnotator myAbstractTypeInfoAwareAn) {
        MetaRuleFactory ruleFactory = new MetaRuleFactory(config, myAbstractTypeInfoAwareAn, new SchemaStore());
        return new SchemaMapper(ruleFactory,new SchemaGenerator());
    }

    /**
     * 默认总体配置+注解配置
     *
     * @return
     */
    public static SchemaMapper getDefaultSchemaMapper() {
        GenerationConfig generationConfig = getGenerationConfig();
        MetaAbstractTypeInfoAnnotator annotator = getAnnotator(generationConfig);
        return getSchemaMapper(generationConfig, annotator);
    }

    public static void main(String[] args) {
        // JSON数据
        String json = "{\"name1\":\"John\", \"age\":30}";

        // JSON Schema定义
        String schemaStr="{\"minProperties\":1,\"type\":\"object\",\"$comment\":\"this is a mark for our injected plugin schema\",\"properties\":{\"_meta\":{\"type\":\"object\",\"properties\":{\"error_response\":{\"oneOf\":[{\"type\":\"string\"},{\"type\":\"object\"}]},\"filter\":{\"type\":\"array\",\"description\":\"filter determines whether the plugin needs to be executed at runtime\"},\"priority\":{\"type\":\"integer\",\"description\":\"priority of plugins by customized order\"},\"disable\":{\"type\":\"boolean\"}}},\"abort\":{\"type\":\"object\",\"required\":[\"http_status\"],\"properties\":{\"percentage\":{\"type\":\"integer\",\"maximum\":100,\"minimum\":0},\"headers\":{\"type\":\"object\",\"patternProperties\":{\"^[^:]+$\":{\"oneOf\":[{\"type\":\"string\"},{\"type\":\"number\"}]}},\"minProperties\":1},\"vars\":{\"type\":\"array\",\"maxItems\":20,\"items\":{\"type\":\"array\"}},\"body\":{\"type\":\"string\",\"minLength\":0},\"http_status\":{\"type\":\"integer\",\"minimum\":200}}},\"delay\":{\"type\":\"object\",\"required\":[\"duration\"],\"properties\":{\"percentage\":{\"type\":\"integer\",\"maximum\":100,\"minimum\":0},\"duration\":{\"type\":\"number\",\"minimum\":0},\"vars\":{\"type\":\"array\",\"maxItems\":20,\"items\":{\"type\":\"array\"}}}}}}";

        JCodeModel codeModel = new JCodeModel();

        try {
            SchemaMapper mapper = MetaJsonschema2pojoConfig.getDefaultSchemaMapper();
            mapper.generate(codeModel, "ClassName", "com.example", schemaStr);
            codeModel.build(Files.createTempDirectory("required").toFile());
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}